//
//  RGFocusView.m
//  RedGrass
//
//  Created by wukong on 2018/11/26.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGFocusView.h"

@interface RGFocusView()<CAAnimationDelegate>

@property (assign, nonatomic) BOOL isFoucs;

@end

@implementation RGFocusView

- (instancetype)init {
    return [self initWithFrame:CGRectMake(0, 0, 24, 24)];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self) {
        self.layer.cornerRadius = frame.size.width/2;
       // self.layer.backgroundColor = UICOLOR_WITH_RGBINT(0xF12F54).CGColor;
        self.image = [UIImage imageNamed:@"关注"];
        self.contentMode = UIViewContentModeCenter;
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBegin)]];
    }
    return self;
}

-(void)tapBegin{
    if(self.focusBtnClickBlock){
        self.focusBtnClickBlock();
    }
}

#pragma mark - public

-(void) begainAnimationWithIsFocus:(BOOL)focus{
    _isFoucs = focus;
    [self beginAnimation];
}

-(void) updateOriginalUiStyleWithIsfocus:(BOOL)focus{
    _isFoucs = focus;
    if(focus){
        self.image = [UIImage imageNamed:@"已关注_mainPage"];
    }else{
        self.image = [UIImage imageNamed:@"关注"];
    }
}


#pragma mark - private
-(void)beginAnimation {
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.delegate = self;
    animationGroup.duration = 1.0;
    animationGroup.removedOnCompletion = NO;
    animationGroup.fillMode = kCAFillModeForwards;
    animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animationGroup.autoreverses = YES;
    CAKeyframeAnimation *scaleAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnim.autoreverses = YES;
    [scaleAnim setValues:@[
                           [NSNumber numberWithFloat:1.0f],
                           [NSNumber numberWithFloat:1.1f],
                           [NSNumber numberWithFloat:1.1f],
                           [NSNumber numberWithFloat:1.1f],
                           [NSNumber numberWithFloat:1.1f],
                           [NSNumber numberWithFloat:1.1f],
                           [NSNumber numberWithFloat:1.1f],
                           [NSNumber numberWithFloat:0.0f]]];
    
    CAKeyframeAnimation *rotationAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    rotationAnim.autoreverses = YES;
    [rotationAnim setValues:@[
                              [NSNumber numberWithFloat:-1.5f*M_PI],
                              [NSNumber numberWithFloat:0.0f],
                              [NSNumber numberWithFloat:0.0f],
                              [NSNumber numberWithFloat:0.0f]]];
    
    CAKeyframeAnimation * opacityAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    [opacityAnim setValues:@[
                             [NSNumber numberWithFloat:0.8f],
                             [NSNumber numberWithFloat:1.0f],
                             [NSNumber numberWithFloat:1.0f]]];
    opacityAnim.autoreverses = YES;
//    [animationGroup setAnimations:@[scaleAnim,
//                                    rotationAnim,
//                                    opacityAnim]];
    [animationGroup setAnimations:@[scaleAnim,
                                    opacityAnim]];
    
    [self.layer addAnimation:animationGroup forKey:nil];
}


- (void)animationDidStart:(CAAnimation *)anim {
    self.userInteractionEnabled = NO;
    self.contentMode = UIViewContentModeScaleAspectFill;
    self.layer.backgroundColor = UICOLOR_WITH_RGBINT(0xF12F54).CGColor;
    self.image = [UIImage imageNamed:@"iconSignDone"];
}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.userInteractionEnabled = YES;
    self.contentMode = UIViewContentModeScaleAspectFill;
    [self setHidden:NO];
    if(!_isFoucs){
        self.image = [UIImage imageNamed:@"关注"];
    }else{
        self.image = [UIImage imageNamed:@"已关注_mainPage"];
    }
  
}


- (void)resetView {
    self.layer.backgroundColor = UICOLOR_WITH_RGBINT(0xF12F54).CGColor;
    self.image = [UIImage imageNamed:@"关注"];
    [self.layer removeAllAnimations];
    [self setHidden:NO];
}

@end
